package com.hurt.competition.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hurt.competition.admin.dto.ApiResult;
import com.hurt.competition.admin.dto.StudentReq;
import com.hurt.competition.admin.util.ApiResultHandler;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hurt.competition.admin.service.StudentService;
import com.hurt.competition.common.util.CoreResultBuilder;
import com.hurt.competition.common.vo.CoreResult;
import com.hurt.competition.player.service.CompetitionJudgePlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/stu")
public class StudentController {
    @Autowired
    private StudentService studentService;

    private CompetitionJudgePlayerService competitionJudgePlayerService;
    @GetMapping("/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page /*, @PathVariable Integer size*/) {
        Page<StudentReq> studentPage = new Page<>(page, 10);
        IPage<StudentReq> res = studentService.findAll(studentPage);
        return ApiResultHandler.buildApiResult(200, "分页查询所有学生", res);
    }

    @GetMapping("/{studentId}")
    public ApiResult findById(@PathVariable("p_id") Integer studentId) {
        StudentReq res = studentService.findById(studentId);
        if (res != null) {
            return ApiResultHandler.buildApiResult(200,"请求成功",res);
        } else {
            return ApiResultHandler.buildApiResult(404,"查询的用户不存在",null);
        }
    }

    @DeleteMapping("/student/{studentId}")
    public ApiResult deleteById(@PathVariable("p_id") Integer studentId) {
        return ApiResultHandler.buildApiResult(200,"删除成功",studentService.deleteById(studentId));
    }

    @PutMapping("/student")
    public ApiResult update(@RequestBody StudentReq student) {
        int res = studentService.update(student);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"更新成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"更新失败",res);
    }
    @PutMapping("/student/update")
    public CoreResult updates(@RequestBody Integer competitionId) {
        competitionJudgePlayerService.updatePlayerScore(competitionId);
        return CoreResultBuilder.success("成功");
    }
}
